<?php
include('/var/www/html/new/d/module/api/function.class.php');
$data=json_encode($_REQUEST);
$ip = GetIP();

err($data."|".$ip,'get_data');

$s1=new SoapClient("http://open.12301.cc/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$s = new SoapClient(null,array(
	"location" => "http://localhost/open/openService/pft_insideMX.php",
	"uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
);

/*查询http://www.12301.cc/meituan/a.php?orderid=201510270001&method=query&verifycodes=641077,967851&timestamp=2014-05-31+00%3A00%3A00&app_key=ea59253a4292cbff10e3bb1ba1db01a490aabcf0&sign=2b4a885d3d98e117dd043a62aea4044e9986e6ad*/

$method = $_GET['method'];
$app_key = 'ea59253a4292cbff10e3bb1ba1db01a490aabcf0';//唯一标识符

if($method=='send'){	//这里是提交订单

	$phone	= '13675094589'; 		//手机号码
	$orderid= 'wxh2015110200010001854321'; 		//远端订单号
	$ids	= '46888|10675|22219';	//门票id
	$idss	= explode('|',$ids);
	$landid	= $idss[0]; 				//供应商id
	$lid	= $idss[1]; 				//景区id
	$tid	= $idss[2]; 				//门票id
	$num	= '2';				//数量
	$playtime = date("Y-m-d");  		//美团一码一验模式不支持提前预定的门票，默认游玩日期是当天
	$price	= 1;

    $ac1="124612";
    $pw1="d11c3ad8d15920a649185feb09bf87ab"; //票付通账号跟密码
	// $mtaccount = '100006';
	// $mtpwd = '2c168b16db2c0beb17c814075cb42e3d';
	
	$arr = array();
	for($i=1;$i<=$num;$i++)
	{
		$submit_array = array("ac"=>$ac1,"pw"=>$pw1,"in0"=>$lid,"in1"=>$tid,"in2"=>$orderid,"in3"=>$price,"in4"=>1 ,"in5"=>$playtime,"in6"=>'美团',"in7"=>$phone,"in8"=>"","in9"=>1,"in10"=>2,"in11"=>0,"in12"=>"","in13"=>"","in14"=>0,"in15"=>0,"in16"=>$landid);
		$xml2=$s1->__soapCall("PFT_Order_Submit",$submit_array);
		$result=simplexml_load_string($xml2);
		if((string)$result->Rec->UUordernum){ 		//下单成功赋值凭证码，或者出现1075远端订单号重复的情况			
			$codesss=(string)$result->Rec->UUcode;

			$arr[]=$codesss;
		} 
		else{	
			if((int)$result->Rec->UUerrorcode==122){ //授信不足，提醒供应商给分销商授信		
				$str="select mobile from pft_member where id='$landid' limit 1";			
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$aid_Tel=$GLOBALS['le']->f('mobile');
				if($aid_Tel){
					$s->Send_SMS_V($aid_Tel,"系统公告:您的分销商[【美团】北京三快在线科技有限公司]因授信不足下单失败，失败订单号:".$orderid.",请尽快补充授信后联系OTA商家补单，避免影响销量");
				}
			}
			err($orderid.'|'.(int)$result->Rec->UUerrorcode,'send_fail');
			break;
		}
	}
	if($arr){
		$tmp['code']=200;
		$tmp['err_str']='success';
		$tmp['verifycodes']=$arr;
		err(json_encode($tmp)."|".$orderid."|".$ip,'send_success');
		echo json_encode($tmp);
		exit;
	}else{ 
	    $str ='{"code":"300"}';
		echo $str;
		exit;
	}
	
}elseif($method=='cancel'){		//这里是取消订单 ,一次取消一个码

	$code = '644139'; //凭证码
	$orderid = 'wxh2015110200010001654321'; //美团订单号
	$PFT_infunction = new AllFunction();
	//第三方优惠码
	if(strlen($code) != 6){
		$select = "select pftOrder,oStatus from all_api_order  where apiCode='$code' limit 1 ";
		$GLOBALS['le']->query($select);
		if(!$GLOBALS['le']->fetch_assoc()) exit('{"code":"300"}');
		$pt_ordernum = $GLOBALS['le']->f('pftOrder');
		$oStatus = $GLOBALS['le']->f('oStatus');
		if($oStatus == 3 ){
			err($pt_ordernum.'|'.$orderid."|200|".$ip,'cancel_success');
			exit('{"code":"200"}'); //已是取消的状态
		}
		$cancel = $PFT_infunction->Cancel_Order($pt_ordernum,0,-1,0,0,"","","");
		if($cancel == 100){		//取消成功			
			err($pt_ordernum.'|'.$orderid."|200|".$ip,'cancel_success');
			exit('{"code":"200"}');	 
		}else{
			err($pt_ordernum.'|'.$orderid.'|'.$cancel.'|'.$ip,'cancel_fail');
			exit('{"code":"300"}');
		}		
	}
	
	//票付通凭证码
	$reorderid = $orderid;
	$pft_order_xml = $PFT_infunction->Select_REMOorder($reorderid);
	$UUlid= (int)($pft_order_xml->Rec->UUlid);

	$pftOrder = $PFT_infunction->See_Code_More(46888,$code,$UUlid); //1853
	$pftOrder_xml = simplexml_load_string($pftOrder);
	// print_r($pftOrder_xml);exit;
	foreach($pftOrder_xml as $pftOrder_xmls){
		$UUremotenums = explode('_',(string)$pftOrder_xmls->UUremotenum);
		if($UUremotenums[0] == $orderid){
			$pftOrders = (string)$pftOrder_xmls->UUordernum;
			$status = (int)$pftOrder_xmls->UUstatus;
			if($status == 3 ){
				err($pftOrder.'|'.$orderid."|200|".$ip,'cancel_success');
				exit('{"code":"200"}'); //已是取消的状态
			}
			$cancel = $PFT_infunction->Cancel_Order($pftOrders,0,-1,0,0,"","","");
			if($cancel == 100){		//取消成功			
				err($pftOrders.'|'.$orderid."|200|".$ip,'cancel_success');
				exit('{"code":"200"}');
			}else{
				err($pftOrders.'|'.$orderid.'|'.$cancel.'|'.$ip,'cancel_fail');
				exit('{"code":"300"}');
			}
			break;
		}
	}
}
elseif($method=='query'){	//这里是查询订单,一个或者多个查询

	$code = 644139;
	$select = "select pftOrder,oStatus,apiCode from all_api_order  where apiCode in($code) ";
	$GLOBALS['le']->query($select);	
	$api_data = array();
	$arr = array();
	while($row = $GLOBALS['le']->fetch_assoc())	$api_data[] = $row;
	if(count($api_data)){  //查询第三方订单状态
		foreach($api_data as $k){
			if($k['oStatus']==0){//未使用
				$UUstatus = 0;
				$canceled_num = 0;
				$used_num = 0;
			}elseif($k['oStatus']==3){//取消				
				$UUstatus = 2;
				$canceled_num = 1 ;
				$used_num = 0;
			}elseif($k['oStatus']==1){ //已使用
				$UUstatus = 1;
				$used_num = 1;
				$canceled_num = 0;
			}
			$tmp['code']=$k['apiCode'];
			$tmp['status']=$UUstatus;
			$tmp['used_num'] = $used_num;
			$tmp['canceled_num'] = $canceled_num;
			$arr[] =$tmp;
		}		
	}
	else{  //票付通凭证码状态
	
		$orderid = wxh2015110200010001654321; //美团订单号
		$reorderid = wxh2015110200010001654321;
		$stcode = str_replace("，",",",$code);
		$code_arr = explode(',',$stcode);
		$PFT_infunction = new AllFunction();
		$pft_order_xml = $PFT_infunction->Select_REMOorder($reorderid);
		$salerid= (string)($pft_order_xml->Rec->UUsalerid);
		$UUlid= (int)($pft_order_xml->Rec->UUlid);
		foreach($code_arr as $k){
			$pftOrder =  $PFT_infunction->See_Code_More(46888,644139,$UUlid);
            echo '<pre>';
            print_r("fds");
            print_r($code_arr);
            echo '</pre>';
			$pftOrder_xml = simplexml_load_string($pftOrder);
			foreach($pftOrder_xml as $pftOrder_xmls){
				$UUremotenums = explode('_',(string)$pftOrder_xmls->UUremotenum);
				$status = (int)$pftOrder_xmls->UUstatus;
				if($UUremotenums[0] == $orderid){
					if($status == 0  || $status == 2){//未使用或者已过期					
						$UUstatus = 0;
						$canceled_num = 0;
						$used_num = 0;				
					}elseif($status == 3){//取消			 		
						$UUstatus = 2;
						$canceled_num = 1 ;
						$used_num = 0;
					}elseif($status == 1 || $status == 7 ){//已使用或者部分使用					
						$UUstatus = 1;
						$used_num = 1;
						$canceled_num = 0;
					}
					$tmp['code'] = $k;
					$tmp['status'] = $UUstatus;
					$tmp['used_num'] = $used_num;
					$tmp['canceled_num'] = $canceled_num;
					$arr[] =$tmp;
					break;
				}
			}
		}
	}
	err(json_encode($arr).'|'.$orderid .'|'.$ip,'query_response');	
	if($arr){
		$str ='{"code":"200","err_str":"success","result":'.json_encode($arr).'}';
		echo $str;
		exit;
	}
    else{
		exit('{"code":"300","err_str":"fail"}');
	}
}


// function err($txt,$path){
	// $fp = fopen('/var/www/html/new/d/meituan/logs/'.$path.date('Y-m-d').'.txt',"a");
	// flock($fp, LOCK_EX) ;
	// fwrite($fp,date('Y-m-d H:i:s').'|'.$txt."\n");
	// flock($fp, LOCK_UN);
	// fclose($fp);
// }

function err($word,$path){
	$filepath= '/var/www/html/new/d/module/wxh/meituan/logs/'.$path.'.txt';
	if(file_exists($filepath)){
		$word = date("Y-m-d H:i:s")."|".$word;
		$word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
		{
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
			$handler=fopen($filepath,'w');
			flock($handler, LOCK_EX) ;
			fwrite($handler,"\n".$word);
			flock($handler, LOCK_UN);
			fclose($handler);
		}
		else
		{
			$fp = fopen('/var/www/html/new/d/module/wxh/meituan/logs/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}else{
		$fp = fopen('/var/www/html/new/d/module/wxh/meituan/logs/'.$path.'.txt',"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}

function GetIP(){
	if(!empty($_SERVER["HTTP_CLIENT_IP"])){
		$cip = $_SERVER["HTTP_CLIENT_IP"];
	}elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
		$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	}elseif(!empty($_SERVER["REMOTE_ADDR"])){
		$cip = $_SERVER["REMOTE_ADDR"];
	}
	else{
	  $cip = "无法获取！";
	}
	return $cip;
}

?> 